package com.wenzheng.mybatisplus.util;

import java.util.regex.*;

/**
 * @author sky
 * @date 2018/12/05
 */
public class Tool {
	private static final Pattern LINEPATTERN = Pattern.compile("_(\\w)");

	/**
	 * 下划线转驼峰
	 */
	public static String lineToHump(String str) {
		str = str.toLowerCase();
		Matcher matcher = LINEPATTERN.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}

	/**
	 * 驼峰转下划线(简单写法，效率低于{@link #humpToLine2(String)})
	 */
	public static String humpToLine(String str) {
		return str.replaceAll("[A-Z]", "_$0").toLowerCase();
	}

	private static final Pattern HUMPPATTERN = Pattern.compile("[A-Z]");

	/**
	 * 驼峰转下划线,效率比上面高
	 */
	private static String humpToLine2(String str) {
		Matcher matcher = HUMPPATTERN.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
}

